package com.hz.sys.filter;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 创建人: mcz
 * 创建日期: 2018/10/30 10:08
 * 描述:  富文本过滤器
 * 版本: 1.0
 */
public class XssFilter implements Filter {
    private static Logger logger = LoggerFactory.getLogger(XssFilter.class);
    //是否过滤富文本内容
    private static boolean IS_INCLUDE_RICH_TEXT = false;

    private List<String> excludes = new ArrayList<>();

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,ServletException {
        if(logger.isInfoEnabled()){
            logger.info("xss filter is open");
        }
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        if(handleExcludeURL(req, resp)){
            filterChain.doFilter(request, response);
            return;
        }
        XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest) request,IS_INCLUDE_RICH_TEXT);
        filterChain.doFilter(xssRequest, response);
    }

    private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) {
        if (excludes == null || excludes.isEmpty()) {
            return false;
        }
        String url = request.getServletPath();
        for (String pattern : excludes) {
            Pattern p = Pattern.compile("^" + pattern);
            Matcher m = p.matcher(url);
            if (m.find()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        try {
                String isIncludeRichText = filterConfig.getInitParameter("isIncludeRichText");
                if(StringUtils.isNotBlank(isIncludeRichText)){
                    IS_INCLUDE_RICH_TEXT = BooleanUtils.toBoolean(isIncludeRichText);
                }
                String temp = filterConfig.getInitParameter("excludes");
                if (temp != null) {
                    String[] url = temp.split(",");
                    Collections.addAll(excludes, url);
                }
                if(logger.isInfoEnabled()){
                    logger.info("************************xss过滤器初始化成功************************");
                    logger.info("************************xss过滤器初始化成功************************");
                    logger.info("************************xss过滤器初始化成功************************");
                }
        } catch (Exception e) {
            logger.error("************************初始化xss过滤器异常************************");
        }
    }
    @Override
    public void destroy() {}

}

